import { Result, ThemeMode } from '@shared/types.d';

import backendApi from '@/api/backend-api';

/**
 * 设置主题
 * @param theme 主题
 */
export async function setTheme(theme: ThemeMode) {
  await backendApi.system.app.setTheme(theme);
}

/**
 * 获取主题
 */
export async function getTheme(): Promise<Result<ThemeMode>> {
  return await backendApi.system.app.getTheme();
}

/**
 * 最小化窗口
 */
export async function minimizeWindow(): Promise<Result<boolean>> {
  return await backendApi.system.app.minimizeWindow();
}

/**
 * 最大化/还原窗口
 */
export async function maximizeWindow(): Promise<Result<boolean>> {
  return await backendApi.system.app.maximizeWindow();
}

/**
 * 关闭窗口
 */
export async function closeWindow(): Promise<Result<boolean>> {
  return await backendApi.system.app.closeWindow();
}

/**
 * 恢复窗口
 */
export async function restoreWindow(): Promise<Result<boolean>> {
  return await backendApi.system.app.restoreWindow();
}

/**
 * 检查窗口是否最大化
 */
export async function isWindowMaximized(): Promise<Result<boolean>> {
  return await backendApi.system.app.isWindowMaximized();
}

/**
 * 检查窗口是否最小化
 */
export async function isWindowMinimized(): Promise<Result<boolean>> {
  return await backendApi.system.app.isWindowMinimized();
}

/**
 * 设置窗口是否置顶
 * @param alwaysOnTop 是否置顶
 */
export async function setWindowAlwaysOnTop(alwaysOnTop: boolean): Promise<Result<boolean>> {
  return await backendApi.system.app.setWindowAlwaysOnTop(alwaysOnTop);
}

/**
 * 检查窗口是否置顶
 */
export async function isWindowAlwaysOnTop(): Promise<Result<boolean>> {
  return await backendApi.system.app.isWindowAlwaysOnTop();
}

/**
 * 获取窗口大小
 * @returns Promise<Result<{width: number, height: number}>> 窗口大小
 */
export async function getWindowSize(): Promise<Result<{ width: number; height: number }>> {
  return await backendApi.system.app.getWindowSize();
}

/**
 * 设置窗口大小
 * @param width 窗口宽度
 * @param height 窗口高度
 * @param animate 是否使用动画
 * @returns Promise<Result<boolean>> 操作是否成功
 */
export async function setWindowSize(width: number, height: number, animate?: boolean): Promise<Result<boolean>> {
  return await backendApi.system.app.setWindowSize(width, height, animate);
}

/**
 * 获取窗口位置和大小
 * @returns Promise<Result<{x: number, y: number, width: number, height: number}>> 窗口位置和大小
 */
export async function getWindowBounds(): Promise<Result<{ x: number; y: number; width: number; height: number }>> {
  return await backendApi.system.app.getWindowBounds();
}

/**
 * 设置窗口位置和大小
 * @param bounds 窗口位置和大小
 * @param animate 是否使用动画
 * @returns Promise<Result<boolean>> 操作是否成功
 */
export async function setWindowBounds(
  bounds: { x?: number; y?: number; width?: number; height?: number },
  animate?: boolean
): Promise<Result<boolean>> {
  return await backendApi.system.app.setWindowBounds(bounds, animate);
}

/**
 * 保存常规设置
 * @param settings 常规设置
 * @returns Promise<Result<boolean>> 操作是否成功
 */
export async function saveGeneralSettings(settings: {
  autoStart?: boolean;
  startToTray?: boolean;
  closeToTray?: boolean;
  language?: string;
  autoUpdate?: boolean;
  betaUpdates?: boolean;
  memoryLimit?: number;
  hardwareAcceleration?: boolean;
  showTrayIcon?: boolean;
  soundEffects?: boolean;
}): Promise<Result<boolean>> {
  return await backendApi.system.app.saveGeneralSettings(settings);
}

/**
 * 获取常规设置
 * @returns Promise<Result<GeneralSettings>> 常规设置
 */
export async function getGeneralSettings(): Promise<
  Result<{
    autoStart: boolean;
    startToTray: boolean;
    closeToTray: boolean;
    language: string;
    autoUpdate: boolean;
    betaUpdates: boolean;
    memoryLimit: number;
    hardwareAcceleration: boolean;
    showTrayIcon: boolean;
    soundEffects: boolean;
  }>
> {
  return await backendApi.system.app.getGeneralSettings();
}
